package com.jumper.property.comparer.convert.impl;

import com.alibaba.fastjson.JSON;
import com.jumper.property.comparer.convert.PropertyConverter;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * BigDecimal conveter
 */
@Slf4j
public class BigDecimalPropertyConverter implements PropertyConverter {

    @Override
    public Object convert(Object propertyValue, Field field) {
        if (propertyValue == null) {
            return null;
        }
        try {
            return ((BigDecimal) propertyValue).stripTrailingZeros().toPlainString();
        } catch (Exception e) {
            log.warn("BigDecimal格式转换异常:{}", JSON.toJSONString(propertyValue), e);
        }
        return propertyValue;
    }
}
